/**  
* @Title: MessageServiceImpl.java  
* @Description: TODO(用一句话描述该文件做什么)  
* @author 张鑫
* @phone 17561783855  
* @date 2019年7月29日  
*/
package com.fortune.rainshineservice.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.model.service.MessageModel;
import com.fortune.rainshineservice.mapper.MessageMapper;
import com.fortune.rainshineservice.service.MessageService;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**  
* @ClassName: MessageServiceImpl  
* @Description: TODO(这里用一句话描述这个类的作用)  
* @author 张鑫
* @phone 17561783855
* @date 2019年7月29日  
*    
*/
@Service
public class MessageServiceImpl implements MessageService {
    private static final Logger logger = LoggerFactory.getLogger(MessageServiceImpl.class);

    @Autowired
    private MessageMapper messageMapper;

	@Override
	public MsgModel list(ParmsModel parm) {
		//封装页面搜索条件
        Wrapper<MessageModel> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", parm.getUserId());
        //判断消息类型
		if(parm.getType() != null){
			if (parm.getType() == 3) {
				wrapper.eq("type", parm.getType());
			}
			if (parm.getType() == 2) {
				wrapper.ne("type", 3);
			}
		}
        if(StringUtils.isNotBlank(parm.getSearchValue())){
			wrapper.eq("states", 0);
		}
        Page<MessageModel> orderPage = new Page<>(parm.getPage(), parm.getLimit());
        List<MessageModel> list = Optional.ofNullable(messageMapper.selectPage(orderPage,
                wrapper.orderBy("create_time", false))).orElse(Lists.newArrayList());
        int total = messageMapper.selectCount(wrapper);
        return new MsgModel(total, list);
	}

	@Override
	public void addMessage(MessageModel parm) {
		messageMapper.insert(parm);
	}

	@Override
	public void updateState(Integer id) {
		MessageModel message = messageMapper.selectById(id);
		if(message!= null) {
			message.setStates(1);
			messageMapper.updateAllColumnById(message);
		}
		
	}

	@Override
	public MessageModel findByid(Integer id) {
		MessageModel message = messageMapper.selectById(id);
		String date = DateFormatUtils.format(message.getCreateTime(), "yyyy-MM-dd HH:mm:ss");
		message.setTime(date);
		return message;
	}

	@Override
	public Map<String, Integer> selectCount(Integer id) {
		Wrapper<MessageModel> wrapper = new EntityWrapper<>();
		wrapper.eq("user_id", id);
		wrapper.eq("states", 0);
		Wrapper<MessageModel> wrapper2 = new EntityWrapper<>();
		wrapper2.eq("user_id", id);
		wrapper2.eq("states", 0);
		 
		Integer num12 = messageMapper.selectCount(wrapper.ne("type", 3));
		Integer num3 = messageMapper.selectCount(wrapper2.eq("type", 3));
		
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("num12", num12);
		map.put("num3", num3);

		return map;
	}

	@Override
	public void del(Integer id) {
		messageMapper.deleteById(id);
	}

}
